import 'package:word_range_client/config/RepositoryConfig.dart';
import "package:word_range_client/pojo/Result.dart";
import 'package:word_range_client/repositories/HttpService.dart';

import '../pojo/dto/LoginDto.dart';
import '../pojo/dto/UserDto.dart';

class UserRepository{
  static String get _serverUrl => RepositoryConfig().serverUrl;
  static final HttpService _httpService = HttpService(_serverUrl);



  static Future<Result> login(String phone, String password) async {
    var loginDtoJson = LoginDto(phone: phone, password: password).toJson();
    try{
      // 这里await等待异步完成
      Result result = await _httpService.post('apiV1/user/login', loginDtoJson, headers: {});
      return result;
    }catch(e){
      throw Exception('登录失败: $e');
    }
  }

  static Future<Result> register(String name, String phone, String password) async {
    var userDtoJson = UserDto(name: name, phone: phone, password: password).toJson();
    try{
      Result result = await _httpService.post('apiV1/user/logon', userDtoJson);
      return result;
    }catch(e){
      throw Exception('注册失败: $e');
    }
  }

  static Future<Result> getUserByToken(String jwt) async {
    Map<String,String> headers = { "JWT" : jwt };
    try{
      Result result = await _httpService.get("apiV1/user/info/byJwt",headers: headers);
      return result;
    }catch(e){
      throw Exception(e);
    }
  }

  static Future<Result> getRegisterDays() async {
    try{
      Result result = await _httpService.get("apiV1/user/registerDays");
      return result;
    }catch(e){
      throw Exception(e);
    }
  }

  static Future<Result> updateUser(UserDto userDto) async {
    try{
      Result result = await _httpService.put("apiV1/user/info/ByJwt", userDto.toJson());
      return result;
    }catch(e){
      throw Exception(e);
    }
  }

  static Future<Result> deleteUser() async {
    try{
      Result result = await _httpService.delete("apiV1/user/info/ByJwt");
      return result;
    }catch(e){
      throw Exception(e);
    }
  }

  static Future<Result> getUserActivityIntegral() async {
    try{
      Result result = await _httpService.get("apiV1/user/activityIntegral/ByJwt");
      return result;
    }catch(e){
      throw Exception(e);
    }
  }


}